import Foundation
import Down
import UIKit

public class NodeLogger {
    private let logger = MarkdownLogger.shared
    
    // MARK: - Basic Logging Methods
    
    func debug(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
        logger.debug(message, file: file, function: function, line: line)
    }
    
    func info(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
        logger.info(message, file: file, function: function, line: line)
    }
    
    func warning(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
        logger.warning(message, file: file, function: function, line: line)
    }
    
    func error(_ message: String, file: String = #file, function: String = #function, line: Int = #line) {
        logger.error(message, file: file, function: function, line: line)
    }
    
    // MARK: - Node Visit Logging
    
    func logNodeVisit(_ node: Node, file: String = #file, function: String = #function, line: Int = #line) {
        let nodeType = type(of: node)
        let nodeInfo = """
        Node visit started:
        - Type: \(nodeType)
        - Has children: \(node.children.count > 0)
        - Children count: \(node.children.count)
        """
        logger.debug(nodeInfo, file: file, function: function, line: line)
    }
    
    func logImageNode(_ image: Image, file: String = #file, function: String = #function, line: Int = #line) {
        let imageInfo = """
        Image node details:
        - URL: \(image.url ?? "nil")
        - Title: \(image.title ?? "nil")
        - Has children: \(image.children.count > 0)
        - Children count: \(image.children.count)
        """
        logger.info(imageInfo, file: file, function: function, line: line)
    }
    
    func logLinkNode(_ link: Link, file: String = #file, function: String = #function, line: Int = #line) {
        let linkInfo = """
        Link node details:
        - URL: \(link.url ?? "nil")
        - Title: \(link.title ?? "nil")
        - Has children: \(link.children.count > 0)
        - Children count: \(link.children.count)
        """
        logger.info(linkInfo, file: file, function: function, line: line)
    }
    
    func logTextNode(_ text: Text, file: String = #file, function: String = #function, line: Int = #line) {
        let textInfo = """
        Text node details:
        - Content: \(text.literal ?? "nil")
        - Length: \(text.literal?.count ?? 0)
        """
        logger.debug(textInfo, file: file, function: function, line: line)
    }
    
    func logHeadingNode(_ heading: Heading, file: String = #file, function: String = #function, line: Int = #line) {
        let headingInfo = """
        Heading node details:
        - Level: \(heading.headingLevel)
        - Has children: \(heading.children.count > 0)
        - Children count: \(heading.children.count)
        """
        logger.debug(headingInfo, file: file, function: function, line: line)
    }
    
    func logCodeBlockNode(_ codeBlock: CodeBlock, file: String = #file, function: String = #function, line: Int = #line) {
        let codeInfo = """
        CodeBlock node details:
        - Language: \(codeBlock.literal ?? "nil")
        - Content length: \(codeBlock.literal?.count ?? 0)
        """
        logger.debug(codeInfo, file: file, function: function, line: line)
    }
    
    // MARK: - Rendering Process Logging
    
    func logRenderingStart(_ node: Node, file: String = #file, function: String = #function, line: Int = #line) {
        let info = """
        Starting to render node:
        - Type: \(type(of: node))
        - Memory address:---
        """
        logger.debug(info, file: file, function: function, line: line)
    }
    
    func logRenderingComplete(_ node: Node, view: UIView?, file: String = #file, function: String = #function, line: Int = #line) {
        let info = """
        Completed rendering node:
        - Type: \(type(of: node))
        - Result view: \(String(describing: view))
        - View frame: \(view?.frame ?? .zero)
        """
        logger.debug(info, file: file, function: function, line: line)
    }
    
    func logRenderingError(_ node: Node, error: Error, file: String = #file, function: String = #function, line: Int = #line) {
        let info = """
        Error rendering node:
        - Type: \(type(of: node))
        - Error: \(error.localizedDescription)
        """
        logger.error(info, file: file, function: function, line: line)
    }
    
    // MARK: - Image Processing Logging
    
    func logImageProcessingStart(_ url: URL?, size: CGSize?, file: String = #file, function: String = #function, line: Int = #line) {
        let info = """
        Starting image processing:
        - URL: \(url?.absoluteString ?? "nil")
        - Target size: \(size?.debugDescription ?? "nil")
        """
        logger.debug(info, file: file, function: function, line: line)
    }
    
    func logImageLoadSuccess(_ image: UIImage, url: URL, file: String = #file, function: String = #function, line: Int = #line) {
        let info = """
        Image loaded successfully:
        - URL: \(url)
        - Size: \(image.size)
        - Scale: \(image.scale)
        - Orientation: \(image.imageOrientation.rawValue)
        - Memory size: \(NSData(data: image.jpegData(compressionQuality: 1.0) ?? Data()).count / 1024) KB
        """
        logger.info(info, file: file, function: function, line: line)
    }
    
    func logImageLoadError(_ url: URL?, error: Error, file: String = #file, function: String = #function, line: Int = #line) {
        let info = """
        Image load failed:
        - URL: \(url?.absoluteString ?? "nil")
        - Error: \(error.localizedDescription)
        """
        logger.error(info, file: file, function: function, line: line)
    }
    
    func logImageSizeCalculation(_ originalSize: CGSize, targetSize: CGSize, file: String = #file, function: String = #function, line: Int = #line) {
        let info = """
        Image size calculation:
        - Original size: \(originalSize)
        - Target size: \(targetSize)
        - Scale factor: \(targetSize.width / originalSize.width)
        """
        logger.debug(info, file: file, function: function, line: line)
    }
}

